<script setup>
import { ref, onMounted } from 'vue'
// 引用swiper插件js
import Swiper from 'swiper'
//import 'swiper/css'
import 'swiper/swiper-bundle.css'

// 导入Swiper模块
import {Autoplay, Navigation, Pagination} from 'swiper/modules';
// 注册Swiper模块   官网需要导入的模块 https://swiperjs.com/swiper-api#custom-build
Swiper.use([Autoplay, Navigation, Pagination])

// 定义props接收父级数据
const props = defineProps({
  swiperId: {
    type: String,
    default: 'swiper-container'
  },
  goodsList: {
    type: Array,
    default: () => []
  }
})

// 组件挂载完成后初始化Swiper
const loadSwiper = () => {
  let mySwiper = new Swiper('#'+props.swiperId, {
    //modules: [Autoplay, Navigation, Pagination],
    loop: true,
    // 写了 autoplay: true 等同于下面 autoplay:{delay: 3100,} 的内容 delay不要填整数，会失效
    // autoplay: true,
    autoplay: {
      delay: 3100, // 不要填整数，(3000)会失效
      disableOnInteraction: false,
    },
    slidesPerView: 4,
    spaceBetween: 20,
    pagination: {
      el: `#${props.swiperId} .swiper-pagination`,
      clickable: true,
    },
    navigation: {
      nextEl: `#${props.swiperId} .swiper-button-next`,
      prevEl: `#${props.swiperId} .swiper-button-prev`,
    }
  })
}
onMounted(() => loadSwiper())
</script>

<template>
  <div class="swiper-group">
    <div :id="swiperId" class="swiper-container">
      <div class="swiper-wrapper">
        
        <div class="swiper-slide" v-for="(goods,index) in goodsList" :key="index">
          <RouterLink class="goods-item" :to="`/detail/${goods.id}`">
            <div class="pic"><img v-img-lazy="goods.picture" /></div>
            <div class="info-desc">
              <p class="name ellipsis-2">{{goods.name}}</p>
              <p class="desc ellipsis">{{goods.desc}}</p>
              <p class="price">{{goods.price}}</p>
            </div>
            <div class="extra">
              <span>找相似</span>
              <span>发现现多宝贝 ></span>
            </div>
          </RouterLink>
        </div>
        
      </div>
      <div class="swiper-pagination"></div>
      <div class="swiper-button-prev"><i class="iconfont icon-angle-left"></i></div>
      <div class="swiper-button-next"><i class="iconfont icon-angle-right"></i></div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
// swiper组件样式不生效，需要到全局样式styles文件调整
.swiper-group{
  width: 100%;
  position: relative;
  .swiper-pagination{
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    .swiper-pagination-bullet{
      margin: 0 8px;
      width: 8px;
      height: 8px;
      background: none !important;
      border-radius: 500px;
      border: 1px solid #cecece;
      border-radius: 7px;
      transition: all 0.5s ease;
      opacity: 1;
      cursor: pointer;
    }
    .swiper-pagination-bullet-active{
      -webkit-box-shadow: 0 0 0 4px #dfcead;
      -moz-box-shadow: 0 0 0 4px #dfcead;
      box-shadow: 0 0 0 4px #dfcead;
      background: #a7936e !important;
      border-radius: 500px;
      border: none;
    }
  }
  .swiper-button-prev,
  .swiper-button-next{
    width: 30px;
    height: 30px;
    background-image: unset;
    background-color: rgba(0,0,0,.35);
    border-radius: 50%;
    border: none;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 5;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 1;
    transition: all .4s;
  }
  .swiper-button-prev::after,
  .swiper-button-next::after{
    display: none;
  }
  .swiper-button-prev i,
  .swiper-button-next i{
    font-size: inherit;
    color: #fff;
  }
  .swiper-button-prev:hover,
  .swiper-button-next:hover{
    background-color: #d1aa87;
    color: #fff;
  }
  &:hover .swiper-button-prev,
  &:hover .swiper-button-next{
    opacity: 1;
  }
  .swiper-button-prev{
    left: 1%;
  }
  &:hover .swiper-button-prev{
    left: 3%;
  }
  .swiper-button-next{
    right: 1%;
  }
  &:hover .swiper-button-next{
    right: 3%;
  }
}
.goods-item{
  display: block;
  position: relative;
  overflow: hidden;
  border: 1px solid #ededed;
  transition: all .5s;
  .pic{
    width: 100%;
    height: 225px;
    img{
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  .info-desc{
    padding: 15px;
  }
  p{
    font-size: 14px;
  }
  p.name{
    height: 40px;
  }
  p.desc{
    font-size: 12px;
    color: #999;
    height: 17px;
    margin-top: 5px;
  }
  p.price{
    margin-top: 10px;
    font-size: 20px;
    color: $priceColor;
  }
  .extra{
    position: absolute;
    left: 0;
    bottom: 0;
    height: 86px;
    width: 100%;
    background: #27ba9b;
    text-align: center;
    transform: translate3d(0,100%,0);
    transition: all .5s;
    span{
      display: block;
      color: #fff;
      width: 120px;
      margin: 0 auto;
      line-height: 30px;
    }
    span:first-child{
      font-size: 18px;
      border-bottom: 1px solid #fff;
      line-height: 40px;
      margin-top: 5px;
    }
  }
  &:hover{
    border-color: $xtxColor;
  }
  &:hover .extra{
    transform: none;
  }
}
</style>
